package com.example.testspring;

import jakarta.annotation.PostConstruct;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Enumeration;

/**
 * @Author：anguangpu
 * @Date：2024/3/12 15:54
 * @Description:
 */
@RestController
public class SimpleController {


    //注入了一个httpServletRequest
    @Autowired
    HttpServletRequest autoRequest;

    @RequestMapping("/get")
    public ReturnT get(HttpServletRequest request){
        System.out.println("------------------------------------------");
        Enumeration<String> en=autoRequest.getHeaderNames();
        System.out.println(autoRequest);
        while(en.hasMoreElements()){
            String key;
            System.out.println((key=en.nextElement()) +" = "+autoRequest.getHeader(key));
        }
        System.out.println("=========================================");
        Enumeration<String> en2=request.getHeaderNames();
        System.out.println(request);
        while(en2.hasMoreElements()){
            String key;
            System.out.println((key=en2.nextElement()) +" = "+request.getHeader(key));
        }

        String uuid=(String) request.getSession().getAttribute("uuid");
        System.out.println(request.getSession().getId());
        if (uuid==null){
            throw new BusinessException("501","uuid is null");
        }
        return uuid==null?new ReturnT():new ReturnT("200","OK",uuid);
    }

    @PostConstruct
    public void after(){
        //断点这里查看
        System.out.println(this.autoRequest);
    }

}
